package com.ruoyi.oto.controller;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.MemberUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.oto.constants.CollectionModle;
import com.ruoyi.oto.constants.CompanyShowFlag;
import com.ruoyi.oto.constants.CompanyStatus;
import com.ruoyi.oto.constants.DeleteFlag;
import com.ruoyi.oto.domain.ModelCollection;
import com.ruoyi.oto.domain.CompanyCore;
import com.ruoyi.oto.domain.CompanyType;
import com.ruoyi.oto.domain.dto.CompanyQuryDTO;
import com.ruoyi.oto.domain.dto.CompanyRegDTO;
import com.ruoyi.oto.domain.vo.CompanyTypeVO;
import com.ruoyi.oto.service.CompanyCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 小程序-一对一企业管理
 */
@RestController
@RequestMapping("/api/v2/company")
public class CompanyApiController extends BaseController {

    /**
     * 企业服务
     */
    @Autowired
    CompanyCoreService companyCoreService;

    /**
     * 分页获取企业
     * @param quryDTO
     * @return
     */
    @PostMapping("/companyList")
    public AjaxResult<IPage<CompanyCore>> companyList(@RequestBody CompanyQuryDTO quryDTO) {
        quryDTO.setShowFlag(CompanyShowFlag.显示);
        quryDTO.setStatus(CompanyStatus.审核通过);
        IPage<CompanyCore> data =  companyCoreService.pageCompany(quryDTO);
        return AjaxResult.success(data);
    }

    /**
     * 企业注册
     * @param companyRegDTO
     * @return
     */
    @PostMapping("/regCompany")
    public AjaxResult<Long> regCompany(@Valid @RequestBody CompanyRegDTO companyRegDTO) {
        MemberUser member = this.getLoginUser().getMember();
        Long companyId =  companyCoreService.regCompany(member,companyRegDTO);
        return AjaxResult.success(companyId);
    }

    /**
     * 企业类型列表
     * @return
     */
    @GetMapping("companyTypeList")
    public AjaxResult<List<CompanyTypeVO>> companyTypeList() {
        CompanyType companyType = new CompanyType();
        List<CompanyType> list = companyType.selectList(new LambdaQueryWrapper<CompanyType>().eq(CompanyType::getDelFlag, DeleteFlag.NO));
        List<CompanyTypeVO> result=  new ArrayList<>();
        list.forEach(item -> {
            result.add(new CompanyTypeVO(item.getId(),item.getTypeName()));
        });
        return AjaxResult.success(result);
    }

    /**
     * 企业详情
     * @param companyId 企业ID
     * @return
     */
    @GetMapping("companyDetail/{companyId}")
    public AjaxResult<CompanyCore> companyDetail(@PathVariable Long companyId) {
        if(companyId==-1){  //代表查当前登录用户管理的企业
                CompanyCore companyCore = new CompanyCore();
                companyCore = companyCore.selectOne(new LambdaQueryWrapper<CompanyCore>().eq(CompanyCore::getMemberId,this.getLoginUser().getMember().getId()));
                return AjaxResult.success(companyCore);
        }
        else{
            CompanyCore companyCore = new CompanyCore().selectById(companyId);
            ModelCollection modelCollection = new ModelCollection().selectOne(new LambdaQueryWrapper<ModelCollection>().eq(ModelCollection::getModelName, CollectionModle.企业));
            companyCore.setCollect(ObjUtil.isNotNull(modelCollection));
            return AjaxResult.success(companyCore);
        }
    }

    /**
     * 编辑企业
     * @param companyRegDTO 企业ID
     * @return
     */
    @PostMapping("editCompany")
    public AjaxResult<Long> editCompany(@Valid @RequestBody CompanyRegDTO companyRegDTO) {
        if(companyRegDTO.getCompanyId()==null){
            throw new ServiceException("企业ID不能为空");
        }
        Long companyId = companyCoreService.editCompany(this.getLoginUser(),companyRegDTO);
        return AjaxResult.success(companyId);
    }

    /**
     * 我联络的企业
     * @return
     */
    @GetMapping("/myContactCompany")
    public AjaxResult<List<CompanyCore>> myContactCompany() {
        LoginUser loginUser = this.getLoginUser();
        List<CompanyCore> list =  companyCoreService.myContactCompany(loginUser);
        return AjaxResult.success(list);
    }

    /**
     * 我的企业
     * @return
     */
    @GetMapping("/myCompany")
    public AjaxResult<CompanyCore> myCompany() {
        LoginUser loginUser = this.getLoginUser();
        CompanyCore companyCore =  companyCoreService.getCompanyByMemberId(loginUser.getMember().getId());
        return AjaxResult.success(companyCore);
    }

}
